define(
		[ "PDAppDir/properties", "PDUtilDir/util", "PDUtilDir/tool",
				"PDUtilDir/slidebar", "PDUtilDir/inputSelect",
				"css!AppPathDir/product/productInfos/views/css/css" ],
		function(Properties, Util, Tool, Slidebar, inputSelect) {
			function addSupportInit($scope, $compile) {
				return new addSupport($scope, $compile);
			}
			function addSupport($scope, $compile) {
				this.$scope = $scope;
				this.$compile = $compile;
			}
			addSupport.fn = addSupport.prototype;
			addSupport.fn.init = function() {
				this.entity = {
					spots : new Array()
				};
				this.cache = {
					path: Properties.URL_FILE_SERVER_URL
				}
				this.bind = {
					spotIds : new Array(),
					spots : new Array()
				};
			};
			addSupport.fn.digest = function() {
				this.$scope.$digest();
			};
			addSupport.fn.run = function() {
				var _this = this;
				_this.init();
				this.slidebars = Slidebar({
					size : 'lg',
					url : getServer()
							+ "/static/app/product/lineInfos/views/add.html",
//					close : true,
					$scope : this.$scope,
					$compile : this.$compile,
					afterLoad : function() {
						// console.info(_this);
						_this.initialization();
					}
				});
			};
			addSupport.fn.initialization = function() {
				this.baseInit();
				this.spotsInit();
			};

			addSupport.fn.baseInit = function() {
				var _this = this;
				
				$.ajax({
					url : getServer()
							+ "/flea/hclv/getUUID.htmls",
					method : 'get',
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						_this.entity.code = data.data
					}
				});
				
				inputSelect.dropCache();
				inputSelect({
					id : "typeName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.types,
					simpleData : true,
					searchAble : false,
					callback : function(id, data) {
						_this.entity.type = id[0];
						_this.$scope.addLineForm.type.$setViewValue(_this.entity.type);
					}
				});
				$("#spotsLineInfo .generate_line .spots .list").mCustomScrollbar({
					axis : "y",
					theme: "minimal-dark"
				});
			};
			addSupport.fn.spotsInit = function() {
				var _this = this;
				// 查询景点
				$.ajax({
					url : getServer()
							+ "/flea/hclv/spot/selectMySpot.htmls",
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						var spots = _this.bind.spots = data.data, len = spots.length;
//						for (var i = 0; i < len; i++) {
//							spots[i].path = Properties.URL_FILE_SERVER_URL + spots[i].path;
//						}
						_this.digest();
					}
				});
			};

			addSupport.fn.choiceFun = function(id) {
				var _this = this, ids = _this.bind.spotIds, index = ids.indexOf(id), spots = undefined, i = 0, len = 0;
				if (index >= 0) {
					spots = _this.entity.spots;
					len = spots.length;

					for (i; i < len; i++) {
						if (id == spots[i].id) {
							spots.splice(i, 1);
							ids.splice(index, 1)
						}
					}
					return;
				}
				spots = _this.bind.spots;
				len = spots.length;
				
				for (i; i < len; i++) {
					if (id == spots[i].id) {
						_this.entity.spots.push(spots[i]);
						_this.bind.spotIds.push(id);
						break;
					}
				}
				spots.splice(i, 1);
			};
			addSupport.fn.unChoiceFun = function(id) {
				var spots = this.entity.spots, len = spots.length;
				
				for (var i = 0; i < len; i++) {
					if (id == spots[i].id) {
						this.bind.spots.push(spots[i])
						spots.splice(i, 1);
						this.bind.spotIds.splice(i, 1);
						return;
					}
				}
			};
			addSupport.fn.save = function() {
				var _this = this;
//				if ( _this.entity.spots.length == 0) {
//					Util.confirm("未选择景点，确认？", function() {
//						console.info('ok');
//					},function(){
//						return;
//					});
//				}
				$.ajax({
					url : getServer() + "/flea/hclv/line/save.htmls",
					data : {
						'model' : JSON.stringify(_this.entity)
					},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						_this.$scope.grid.refresh();
						Util.alert(data.message);
						_this.slidebars.close();
					}
				});
			};
			return addSupportInit;
		});